<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.HisiPHP.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------

namespace app\common\service;

use app\common\service\Base;
use think\facade\Cache;

class Storeshelf extends Base
{

	/**
	 * 获取键值对缓存
	 *
	 * @author KING
	 * @date 2020年6月9日
	 *
	 * @return array
	 */
	public function getKVCache($storehouseId=0) {
		if ($storehouseId) $key = KV_STORESHELF .'_'. $storehouseId;
		else $key = KV_STORESHELF;
		$data = cache($key);
		if(!$data) {
			$this->updateKVCache();
		}
		$data = cache($key);
		return $data;
	}

	/**
	 * 更新键值对缓存
	 *
	 * @author KING
	 * @date 2020年6月9日
	 *
	 */
	public function updateKVCache() {
		$storehouseKV = (new \app\common\service\Storehouse())->getKVCache();
		foreach ($storehouseKV as $storehouseId=>$v) {
			$list = $this->getKVList($storehouseId);
			cache(KV_STORESHELF . '_' . $storehouseId, $list);
		}
		$list = $this->getKVList();
		cache(KV_STORESHELF, $list);
		return $list;
	}

	/**
	 * 获取键值对列表
	 *
	 * @author KING
	 * @date 2020年6月9日
	 *
	 * @param integer $status
	 * @return array
	 */
	public function getKVList($storehouseId=0) {
		$where = [];
		if ($storehouseId) $where[] = ['storehouse_id', '=', $storehouseId];
		$order = "id desc";
		return db('storeshelf')->where($where)->order($order)->column('name', 'id');
	}
}